package com.cloudkinto.inteceptors;


import org.apache.commons.lang3.StringUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/*******************************************************************
  fanting  2019/4/10
 */
public class RequestHolder {
    private static ThreadLocal<HttpServletRequest> requestLocal = new ThreadLocal<>();
    private static ThreadLocal<HttpServletResponse> responseLocal = new ThreadLocal<>();

    public static void clear() {
        requestLocal.remove();
        responseLocal.remove();
    }


    public static HttpServletRequest getRequestLocal() {
        return requestLocal.get();
    }

    public static void setRequestLocal(HttpServletRequest requestLocal) {
        RequestHolder.requestLocal.set(requestLocal);
    }

    public static HttpServletResponse getResponseLocal() {
        return responseLocal.get();
    }

    public static void setResponseLocal(HttpServletResponse responseLocal) {
        RequestHolder.responseLocal.set(responseLocal);
    }

    public static String getString(HttpServletRequest request, String key, String defaultValue) {
        String value = request.getParameter(key);
        return StringUtils.isEmpty(value) ? defaultValue : value.replace("'", "''").trim();
    }
}
